package com.xbongbong.paas.help.analysis.data;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.MessageConstant;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.enums.PlatFormEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.MultiUnitItemPoJo;
import com.xbongbong.paas.field.UnitItemPoJo;
import com.xbongbong.paas.help.UserHelper;
import com.xbongbong.paas.pojo.dto.AnalysisDataDTO;
import com.xbongbong.paas.pojo.dto.FormDataListDTO;
import com.xbongbong.paas.pojo.dto.ParseSingleRowDataDTO;
import com.xbongbong.paas.pojo.vo.LinkResultDataDTO;
import com.xbongbong.paas.pojo.vo.LinkResultDataVO;
import com.xbongbong.paas.pojo.vo.PaasFormDataEsListVO;
import com.xbongbong.paas.pojo.vo.ParseSingleRowDataVO;
import com.xbongbong.paas.service.CoUserService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.parent.parse.data.FormDataFastUpdateAnalysisDataHelp;
import com.xbongbong.parent.parse.data.FormDataLinkDataListServiceImpl;
import com.xbongbong.parent.parse.data.FormDataListAnalysisDataHelp;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.form.pojo.dto.FormConfigGetDTO;
import com.xbongbong.pro.label.pojo.LabelPojo;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.ParameterConstant;
import com.xbongbong.saas.constant.ProductConstant;
import com.xbongbong.saas.constant.StockConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.enums.FormConfigEnum;
import com.xbongbong.saas.enums.OperateTypeEnum;
import com.xbongbong.saas.enums.UserTeamEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.CustomerManagementEnum;
import com.xbongbong.saas.enums.business.ProductEnum;
import com.xbongbong.saas.help.FormDataAnalysisHelp;
import com.xbongbong.saas.help.LabelHelp;
import com.xbongbong.saas.help.RelativeProductHelp;
import com.xbongbong.saas.service.UserTeamService;
import com.xbongbong.saas.service.toolbox.help.AnalysisSaasDataListHelp;
import com.xbongbong.saas.service.toolbox.help.AttrDecimalPrecisionHelper;
import com.xbongbong.sys.domain.entity.DepartmentEntity;
import com.xbongbong.sys.domain.entity.UserEntity;
import com.xbongbong.sys.model.DepartmentModel;
import com.xbongbong.sys.model.UserModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * 云叩的列表解析
 * @author: wufeng
 * @date: 2018/12/5 9:44
 * @desrcption: 解析数据
 */
@Component
public class FormDataCloudCodeDataListAnalysisDataHelp extends FormDataListAnalysisDataHelp {

    private static final Logger LOG = LoggerFactory.getLogger(FormDataCloudCodeDataListAnalysisDataHelp.class);

    @Resource
    private FormDataAnalysisHelp formDataAnalysisHelp;
    @Resource
    private UserTeamService userTeamService;
    @Resource
    private CoUserService coUserService;
    @Resource
    private FormDataLinkDataListServiceImpl formDataLinkDataListService;
    @Resource
    private UserModel userModel;
    @Resource
    private DepartmentModel departmentModel;
    @Resource
    private RelativeProductHelp relativeProductHelp;
    @Resource
    private FormDataFastUpdateAnalysisDataHelp formDataFastUpdateAnalysisDataHelp;
    @Resource
    private LabelHelp labelHelp;
    @Resource
    private AttrDecimalPrecisionHelper attrDecimalPrecisionHelper;
    @Resource
    private AnalysisSaasDataListHelp analysisSaasDataListHelp;

    /**
     * 解析数据
     * @param formDataListVO
     * @throws XbbException
     */
    @Override
    public void analysisDataList(FormDataListDTO formDataListDTO, PaasFormDataEsListVO formDataListVO) throws XbbException {

        try {
            List<PaasFormDataEntityExt> formDataEsList = formDataListVO.getPaasFormDataESList();
            List<FieldAttrEntity> explainList = formDataListVO.getHeadList();
            Set<String> userAttrSet = new HashSet<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            explainList.forEach(fieldAttrEntity -> {
                Integer fieldType = fieldAttrEntity.getFieldType();
                if ( Objects.equals(fieldType,FieldTypeEnum.USER.getType()) || Objects.equals(fieldType,FieldTypeEnum.USER_GROUP.getType())
                        || Objects.equals(fieldType,FieldTypeEnum.CREATORID.getType()) || Objects.equals(fieldType,FieldTypeEnum.OWNERID.getType())
                        || Objects.equals(fieldType,FieldTypeEnum.COUSERID.getType())) {
                    userAttrSet.add(fieldAttrEntity.getAttr());
                }
            });
            List<Long> dataIdList = new ArrayList<>();
            Set<String> userIdList = new HashSet<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            for(PaasFormDataEntityExt item: formDataEsList) {
                dataIdList.add(item.getDataId());
                for ( String userAttr : userAttrSet ) {
                    Object userIdObject = item.getData().get(userAttr);
                    if ( Objects.isNull(userIdObject) ) {
                        continue;
                    }
                    if ( userIdObject instanceof List ) {
                        List<String> userIdArray = JSONArray.parseArray(userIdObject.toString(), String.class);
                        if ( CollectionsUtil.isNotEmpty(userIdArray) ) {
                            userIdList.addAll(userIdArray);
                        }
                    } else {
                        if ( Objects.nonNull(userIdObject) ) {
                            userIdList.add(userIdObject.toString());
                        }
                    }
                }
                userIdList.add(item.getCreatorId());
                userIdList.add(item.getOwnerId());
            }
            String corpid = formDataListDTO.getCorpid();

            //协同团队
            Map<Long, List<String>> mainUserMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            Map<Long, List<String>> coUserMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            Map<Long, List<String>> paasCoUserMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            Integer businessType = formDataListDTO.getBusinessType();
            Integer subBusinessType = formDataListDTO.getSubBusinessType();
            if (UserTeamEnum.hasTeam(businessType)){
                Boolean isPublic = (Objects.equals(XbbRefTypeEnum.CUSTOMER_PUBLIC.getCode(),subBusinessType) || Objects.equals(XbbRefTypeEnum.CLUE_PUBLIC.getCode(),subBusinessType));
                userTeamService.getUserIdMapSetShow(userIdList,dataIdList,corpid,businessType,isPublic,mainUserMap,coUserMap);
            } else {
                paasCoUserMap = coUserService.coUserMapShow(userIdList,dataIdList, corpid);
            }

            List<UserEntity> userEntityList = new ArrayList<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            Map<String, Object> param = BeanUtil.convertBean2Map(formDataListDTO, true);
            param.remove("del");
//            param.put("del",0);
            param.put("userIdIn",userIdList);
            param.put(ParameterConstant.COLUMNS, "user_id, name");
            param.put(ParameterConstant.LEAVE_MARKS,BasicConstant.ONE);
            List<UserEntity> userList = userModel.findEntitys(param);
            userEntityList.addAll(userList);

            if(userEntityList == null) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.USER_NOT_EXIST);
            }
            Map<String, UserEntity> userMap = new HashMap<>(userEntityList.size());
            userEntityList.forEach((item)->{
                userMap.put(item.getUserId(), item);
            });

            Map<String, DepartmentEntity> departmentMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);

            boolean departmentMapIsEmpty = hasDepartment(explainList);
            if(departmentMapIsEmpty){
                param.clear();
                param = BeanUtil.convertBean2Map(formDataListDTO, true);
                param.remove("del");
                param.put(ParameterConstant.COLUMNS, "id, name");
                List<DepartmentEntity> departmentList = departmentModel.findEntitys(param);
                if (departmentList == null) {
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.DEPT_NOT_EXIST);
                }
                departmentList.forEach((departmentItem) -> {
                    departmentMap.put(departmentItem.getId().toString(), departmentItem);
                });
            }
            Map<String, FieldAttrEntity> explainMap = new HashMap<>(formDataListVO.getHeadList().size());
            for (FieldAttrEntity item : formDataListVO.getHeadList()) {
                explainMap.put(item.getAttr(), item);
            }
            LinkResultDataVO linkResultDataVO = new LinkResultDataVO();
            Set<Object> linkDataSet = new HashSet<>();
            List<PaasFormDataEntityExt> linkDataList = new ArrayList<>();


            Map<Long, Boolean> contactPhoneMap = null;
            // 客户主联系人处理
            if(Objects.equals(SaasMarkEnum.SAAS.getCode(), formDataListDTO.getSaasMark()) &&
                    Objects.equals(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(), formDataListDTO.getBusinessType())) {
                contactPhoneMap = analysisContactForCustomer(corpid, formDataListDTO, dataIdList);
            }

            FormConfigGetDTO formConfigGetDTO = new FormConfigGetDTO();
            formConfigGetDTO.setCorpid(corpid);
            formConfigGetDTO.setBusinessType(businessType);
            formConfigGetDTO.setFormId(formDataListDTO.getFormId());
            Map<String, String> lastConnectTimeMap = lastConnectTime(formConfigGetDTO);
            // 标签处理
            List<FieldAttrEntity> headList = formDataListVO.getHeadList();
            String labelAttr =null;
            for (FieldAttrEntity fieldAttrEntity : headList) {
                if (Objects.equals(fieldAttrEntity.getFieldType(), FieldTypeEnum.LABEL.getType())) {
                    labelAttr = fieldAttrEntity.getAttr();
                    break;
                }
            }

            boolean isStockSearch = Objects.equals(formDataListDTO.getBusinessType(), XbbRefTypeEnum.PRODUCT.getCode()) && StockConstant.STOCK_SEARCH_TYPE_LIST.contains(formDataListDTO.getSubBusinessType());
            Map<Long, MultiUnitItemPoJo> groupBaseUnitMap = relativeProductHelp.getGroupBaseUnitMap(corpid);
            Map<Long, List<UnitItemPoJo>> groupItemsMap = relativeProductHelp.getGroupItemsMap(corpid);
            Map<Long, LabelPojo> labelPojoMap = labelHelp.getLabelMap(formDataListDTO.getFormId(), formDataListDTO.getCorpid(), formDataListDTO.getAppId());
            for (PaasFormDataEntityExt paasFormDataEsEntity: formDataEsList){
                JSONObject dataJsonObject = paasFormDataEsEntity.getData();
                if (Objects.nonNull(paasFormDataEsEntity.getOriginData())) {
                    dataJsonObject = paasFormDataEsEntity.getOriginData();
                    paasFormDataEsEntity.setOriginData(null);
                }
                dataJsonObject.put(FieldTypeEnum.ADDTIME.getAlias(), paasFormDataEsEntity.getAddTime());
                dataJsonObject.put(FieldTypeEnum.UPDATETIME.getAlias(), paasFormDataEsEntity.getUpdateTime());
                dataJsonObject.put(FieldTypeEnum.SERIALNO.getAlias(), paasFormDataEsEntity.getSerialNo());
                // 客户最后跟进时间标识
                if(Objects.equals(businessType, XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode())){
                    dataJsonObject.put(FormConfigEnum.CUSTOMER_LAST_CONNECT_TIME.getDbAlias(), lastConnectTimeMap.get(FormConfigEnum.CUSTOMER_LAST_CONNECT_TIME.getConfig()));

                }
                // 销售机会最后跟进时间标识
                if(Objects.equals(businessType, XbbRefTypeEnum.SALES_OPPORTUNITY.getCode())){
                    dataJsonObject.put(FormConfigEnum.SALES_OPPORTUNITY_LAST_CONNECT_TIME.getDbAlias(), lastConnectTimeMap.get(FormConfigEnum.SALES_OPPORTUNITY_LAST_CONNECT_TIME.getConfig()));
                }
                // 线索最后跟进时间标识
                if(Objects.equals(businessType, XbbRefTypeEnum.CLUE.getCode())){
                    dataJsonObject.put(FormConfigEnum.CLUE_LAST_CONNECT_TIME.getDbAlias(), lastConnectTimeMap.get(FormConfigEnum.CLUE_LAST_CONNECT_TIME.getConfig()));
                }

                JSONObject cloneData = (JSONObject)dataJsonObject.clone();
                ParseSingleRowDataDTO parseSingleRowDataDTO = new ParseSingleRowDataDTO();
                BeanUtil.copyProperties(formDataListDTO, parseSingleRowDataDTO);
                dataJsonObject.put(FieldTypeEnum.CREATORID.getAlias(), paasFormDataEsEntity.getCreatorId());
                // 设置创建人
                if (Objects.equals(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(), businessType)) {
                    parseSingleRowDataDTO.setCreatorId(dataJsonObject.getString(CustomerManagementEnum.CREATOR.getAttr()));
                } else {
                    parseSingleRowDataDTO.setCreatorId(paasFormDataEsEntity.getCreatorId());
                }
                setOwnerIdAndCoUserId(businessType, dataJsonObject, parseSingleRowDataDTO, paasFormDataEsEntity, mainUserMap, coUserMap, paasCoUserMap);
                dataJsonObject.put(FieldTypeEnum.DEPARTMENTID.getAlias(), paasFormDataEsEntity.getDepartmentId());
                Long dataId = paasFormDataEsEntity.getId();
                paasFormDataEsEntity.setDataId(dataId);

                parseSingleRowDataDTO.setDataJsonObject(dataJsonObject);
                parseSingleRowDataDTO.setUserMap(userMap);
                parseSingleRowDataDTO.setDepartmentMap(departmentMap);
                parseSingleRowDataDTO.setExplainMap(explainMap);
                parseSingleRowDataDTO.setListFlag(1);
                parseSingleRowDataDTO.setDataId(dataId);
                ParseSingleRowDataVO parseSingleRowDataVO = parseSingleRowData(parseSingleRowDataDTO);
                if(Objects.isNull(parseSingleRowDataVO)){
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.PARSE_SINGLE_ROW_DATA_FAILED);
                }
                linkResultDataVO = parseSingleRowDataVO.getLinkResultDataVO();
                JSONObject resultObject = parseSingleRowDataVO.getResult();
                if (!linkResultDataVO.getLinkDataSet().isEmpty()) {
                    linkResultDataVO.getLinkDataSet().forEach((item -> {
                        linkDataSet.add(item);
                    }));
                }
                // 库存查询多单位
                if (isStockSearch) {
                    if (BasicConstant.ONE.equals(cloneData.getIntValue(ProductEnum.ENABLE_MULTI_UNIT.getAttr()))) {
                        String unitStr = "";
                        if (PlatFormEnum.WEB.getValue().equals(formDataListDTO.getPlatform())) {
                            unitStr = cloneData.getString(ProductEnum.UNIT.getAttr());
                        } else if (PlatFormEnum.DINGTALK.getValue().equals(formDataListDTO.getPlatform())){
                            unitStr = cloneData.getString(ProductEnum.UNIT.getSaasAttr());
                        }
                        if (StringUtil.isNotEmpty(unitStr)) {
                            MultiUnitItemPoJo multiUnitItemPoJo = groupBaseUnitMap.getOrDefault(Long.parseLong(unitStr), new MultiUnitItemPoJo());
                            resultObject.put(ProductEnum.UNIT.getAttr(), multiUnitItemPoJo.getBaseName());
                            List<UnitItemPoJo> unitItemPoJos = groupItemsMap.get(Long.parseLong(unitStr));
                            resultObject.put(ProductConstant.TRANSFORM_UNIT_RATE, unitItemPoJos);
                        }
                    }
                }
                parseSingleRowDataDTO.setDataJsonObject(cloneData);
                // 为了在列表上面可以更新数据，加下这个
                ParseSingleRowDataVO updateDataVo = formDataFastUpdateAnalysisDataHelp.parseSingleRowData(parseSingleRowDataDTO);
                if(Objects.isNull(updateDataVo)){
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.PARSE_SINGLE_ROW_DATA_FAILED);
                }
                JSONObject sourceData = updateDataVo.getResult();

                sourceData.put(FieldTypeEnum.SERIALNO.getAlias(), paasFormDataEsEntity.getSerialNo());
                paasFormDataEsEntity.setSourceData(sourceData);
                formDataListVO.getHeadList().forEach((item) -> {
                    if (!resultObject.containsKey(item.getAttr())) {
                        if (FieldTypeEnum.SUB_FORM.getType().equals(item.getFieldType())
                                || FieldTypeEnum.UPLOAD.getType().equals(item.getFieldType())
                                || FieldTypeEnum.IMAGE.getType().equals(item.getFieldType())
                                || FieldTypeEnum.CONTACT_NUMBER.getType().equals(item.getFieldType())) {
                            resultObject.put(item.getAttr(), new ArrayList<>());
                        } else {
                            resultObject.put(item.getAttr(), "");
                        }
                    }
                });

                // 主联系人电话不可见时，设置为*****
                if(contactPhoneMap != null && contactPhoneMap.containsKey(dataId) && resultObject.containsKey(CustomerManagementEnum.CONTACT_PHONE.getAttr())) {
                    boolean seePhone = contactPhoneMap.get(dataId);
                    if(!seePhone) {
                        JSONArray contactArray = resultObject.getJSONArray(CustomerManagementEnum.CONTACT_PHONE.getAttr());
                        if(Objects.nonNull(contactArray) && !contactArray.isEmpty()) {
                            for(int i = 0; i < contactArray.size(); i++) {
                                JSONObject phoneObj = contactArray.getJSONObject(i);
                                if(phoneObj.containsKey("text_2")) {
                                    phoneObj.put("text_2", "*****");
                                }
                            }
                        }
                        resultObject.put(CustomerManagementEnum.CONTACT_PHONE.getAttr(), contactArray);
                    }
                }
                labelHelp.dealId2Pojo4One(labelAttr,resultObject, labelPojoMap);
                // 线索最后跟进时间标识
                if(Objects.equals(businessType, XbbRefTypeEnum.CLUE.getCode())){
                    dataJsonObject.put(FormConfigEnum.CLUE_LAST_CONNECT_TIME.getDbAlias(), lastConnectTimeMap.get(FormConfigEnum.CLUE_LAST_CONNECT_TIME.getConfig()));
                }
                paasFormDataEsEntity.setData(resultObject);
                // 移动端，客户、机会、合同、报价单 加上 ‘归’ 字标签 (注：web端tagData已经赋过值，不做处理)
                setAchived(formDataListDTO, resultObject, paasFormDataEsEntity);
                //评分列表页展示
                handleWebListScoreShowValue(businessType,paasFormDataEsEntity);
                // 系统字段经历过一次权限的判断，所以这里加下重新赋值
                paasFormDataEsEntity.setSerialNo(resultObject.getString(FieldTypeEnum.SERIALNO.getAlias()));
                paasFormDataEsEntity.setCreatorId(resultObject.getString(FieldTypeEnum.CREATORID.getAlias()));
                paasFormDataEsEntity.setAddTime(resultObject.getLong(FieldTypeEnum.ADDTIME.getAlias()));
                paasFormDataEsEntity.setUpdateTime(resultObject.getLong(FieldTypeEnum.UPDATETIME.getAlias()));
                paasFormDataEsEntity.setOwnerId(resultObject.getString(FieldTypeEnum.OWNERID.getAlias()));
                paasFormDataEsEntity.setAdvancePaymentBalance(paasFormDataEsEntity.getAdvancePaymentBalance());
                if (Objects.isNull(resultObject.get(FieldTypeEnum.DEPARTMENTID.getAlias()))) {
                    paasFormDataEsEntity.setDepartmentId(null);
                }

                linkDataList.add(paasFormDataEsEntity);
            }
            // 获取关联数据
            linkResultDataVO.setExplainMap(explainMap);
            linkResultDataVO.setUserMap(userMap);
            linkResultDataVO.setDepartmentMap(departmentMap);
            linkResultDataVO.setLinkDataSet(linkDataSet);
            linkResultDataVO.setFormDataList(linkDataList);
            linkResultDataVO.setExplainList(formDataListVO.getHeadList());
            if(!linkDataSet.isEmpty()){
                LinkResultDataDTO linkResultDataDTO = new LinkResultDataDTO();
                BeanUtil.copyProperties(formDataListDTO, linkResultDataDTO);
                formDataLinkDataListService.formDataLinkDataGet(linkResultDataDTO, linkResultDataVO);
            }
            formDataListVO.setPaasFormDataESList(linkResultDataVO.getFormDataList());
//            removeAddress(formDataListVO);
        } catch (XbbException e) {
            LOG.error("formDataListAnalysisDataHelp.analysisData表单列表解析数据出错", e);
            throw e;
        } catch (Exception e) {
            LOG.error("formDataListAnalysisDataHelp.analysisData表单列表解析数据出错", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }

    }

    @Override
    public void analysisData(AnalysisDataDTO analysisDataDTO, JSONObject resultJsonObject,
                             Map<String, Object> linkData, Set<Object> linkDataSet) throws XbbException{
        FieldAttrEntity fieldAttrEntity = analysisDataDTO.getFieldAttrEntity();
        String key = analysisDataDTO.getAttr();
        JSONObject objectJsonObject = analysisDataDTO.getDataJsonObject();
        Map<String, UserEntity> userMap =analysisDataDTO.getUserMap();
        Map<String, DepartmentEntity> departmentMap= analysisDataDTO.getDepartmentMap();
        if (Objects.isNull(analysisDataDTO.getIsProcess()) || !analysisDataDTO.getIsProcess()) {
            FormDataAnalysisHelp.invisibleSetNull(fieldAttrEntity, analysisDataDTO, objectJsonObject);
        }
        if (Objects.equals(FieldTypeEnum.LINK_BUSINESS_SINGLE.getType(), fieldAttrEntity.getFieldType()) && Objects.nonNull(objectJsonObject.get(key))) {
            // 系统业务单选
            String linkedText = fieldAttrEntity.getLinkedText();
            JSONObject jsonObject = new JSONObject();
            JSONArray jsonArray = new JSONArray();
            jsonObject.put(StringConstant.SAAS_LINK_BUSINESS_ID, objectJsonObject.getLong(key));
            jsonObject.put(StringConstant.SAAS_LINK_BUSINESS_NAME, objectJsonObject.getString(linkedText));
            jsonObject.put(StringConstant.SAAS_LINK_BUSINESS_LINK_TYPE, fieldAttrEntity.getLinkedType());
            jsonObject.put("saasMark", SaasMarkEnum.SAAS.getCode());
            jsonArray.add(jsonObject);
            resultJsonObject.put(key, jsonArray);
        } else if (Objects.equals(FieldTypeEnum.LINK_BUSINESS_MULTI.getType(), fieldAttrEntity.getFieldType()) && Objects.nonNull(objectJsonObject.get(key))) {
            // 系统业务多选
            String linkedText = fieldAttrEntity.getLinkedText();
            JSONArray businessIdList  = objectJsonObject.getJSONArray(fieldAttrEntity.getAttr()) == null ? new JSONArray() : objectJsonObject.getJSONArray(fieldAttrEntity.getAttr());
            JSONArray businessNameList = objectJsonObject.getJSONArray(linkedText) == null ? new JSONArray() : objectJsonObject.getJSONArray(linkedText);
            if (!businessIdList.isEmpty() && !businessNameList.isEmpty()) {
                JSONArray jsonArray = new JSONArray();
                for (int i = 0; i < businessIdList.size(); i++) {
                    Long businessId = businessIdList.getLong(i);
                    String businessName = "";
                    if (CollectionsUtil.isNotEmpty(businessNameList) && i < businessNameList.size()) {
                        businessName = businessNameList.getString(i);
                    } else {
                        continue;
                    }
                    JSONObject linkObject = new JSONObject();
                    linkObject.put(StringConstant.SAAS_LINK_BUSINESS_ID, businessId);
                    linkObject.put(StringConstant.SAAS_LINK_BUSINESS_NAME, businessName);
                    linkObject.put(StringConstant.SAAS_LINK_BUSINESS_LINK_TYPE,fieldAttrEntity.getLinkedType());
                    linkObject.put("saasMark", SaasMarkEnum.SAAS.getCode());
                    jsonArray.add(linkObject);
                }
                resultJsonObject.put(key, jsonArray);
            }
        } else if (Objects.equals(FieldTypeEnum.BIRTHDAY.getType(), fieldAttrEntity.getFieldType())) {
            String linkedText = fieldAttrEntity.getLinkedText();
            analysisSaasDataListHelp.formatBirthday4Show(objectJsonObject, key, linkedText);
            resultJsonObject.put(key, objectJsonObject.get(key));
        } else if (Objects.equals(FieldTypeEnum.SWITCH.getType(), fieldAttrEntity.getFieldType())) {
            if (Objects.equals(objectJsonObject.getString(key), BasicConstant.ZERO.toString())) {
                resultJsonObject.put(key, OperateTypeEnum.NO.getName());
            }else if (Objects.equals(objectJsonObject.getString(key), BasicConstant.ONE.toString())){
                resultJsonObject.put(key, OperateTypeEnum.YES.getName());
            }else {
                resultJsonObject.put(key, null);
            }
        } else if((FieldTypeEnum.RADIO_GROUP.getType().equals(fieldAttrEntity.getFieldType())
                || FieldTypeEnum.COMBO.getType().equals(fieldAttrEntity.getFieldType()))
                ||(FieldTypeEnum.CHECKBOX_GROUP.getType().equals(fieldAttrEntity.getFieldType())
                || FieldTypeEnum.COMBO_CHECK.getType().equals(fieldAttrEntity.getFieldType()))) {
            formDataAnalysisHelp.analysisComboJsonObject(objectJsonObject, resultJsonObject, fieldAttrEntity);
        } else if (Objects.equals(FieldTypeEnum.STAGE_THRUSTER.getType(), fieldAttrEntity.getFieldType())) {
            formDataAnalysisHelp.analysisStageJsonObject(analysisDataDTO, objectJsonObject, resultJsonObject, fieldAttrEntity);
        } else if(FieldTypeEnum.USER.getType().equals(fieldAttrEntity.getFieldType())
                || Objects.equals(fieldAttrEntity.getFieldType(), FieldTypeEnum.CREATORID.getType())){
            JSONObject userJson = new JSONObject();
            if(StringUtil.isNotEmpty(objectJsonObject.getString(key))){
                String name = null;
                if(Objects.nonNull(userMap.get(objectJsonObject.getString(key)))){
                    name = userMap.get(objectJsonObject.getString(key)).getName();
                } else if (Objects.equals(objectJsonObject.getString(key), StringConstant.NOT_IN_SYSTEM_ID)) {
                    name = objectJsonObject.getString(fieldAttrEntity.getLinkedText());
                    name = name + "(离职)";
                }
                String userId = null;
                if(Objects.nonNull(userMap.get(objectJsonObject.getString(key)))){
                    userId = userMap.get(objectJsonObject.getString(key)).getUserId();
                }
                userJson.put("name", name);
                userJson.put("userId", userId);
                resultJsonObject.put(key, userJson);
            }else{
                resultJsonObject.put(key, new JSONObject());
            }
        } else if (FieldTypeEnum.COUSERID.getType().equals(fieldAttrEntity.getFieldType())
                || Objects.equals(fieldAttrEntity.getFieldType(), FieldTypeEnum.OWNERID.getType())) {

            List<UserEntity> userList = UserHelper.getUserByKey(key, objectJsonObject, userMap);
            List<JSONObject> nameList = new ArrayList<>();
            userList.forEach(item->{
                JSONObject json = new JSONObject();
                json.put("id", item.getUserId());
                json.put("name", item.getName());
                nameList.add(json);
            });
            resultJsonObject.put(key, nameList);
        }  else if (FieldTypeEnum.USER_GROUP.getType().equals(fieldAttrEntity.getFieldType())) {
            List<UserEntity> userList = UserHelper.getUserByKey(key, objectJsonObject, userMap);
            List<JSONObject> nameList = new ArrayList<>();
            userList.forEach(item->{
                JSONObject json = new JSONObject();
                json.put("id", item.getUserId());
                json.put("name", item.getName());
                nameList.add(json);
            });
            resultJsonObject.put(key, nameList);
        } else if(FieldTypeEnum.DEPT.getType().equals(fieldAttrEntity.getFieldType() )
                || Objects.equals(fieldAttrEntity.getFieldType(), FieldTypeEnum.DEPARTMENTID.getType())){
            if(StringUtil.isNotEmpty(objectJsonObject.getString(key))){
                JSONObject depJson = new JSONObject();
                String name = null;
                Long id = null;
                if(Objects.nonNull(departmentMap.get(objectJsonObject.getString(key)))){
                    id = departmentMap.get(objectJsonObject.getString(key)).getId();
                    name = departmentMap.get(objectJsonObject.getString(key)).getName();
                }
                depJson.put("id", id);
                depJson.put("name", name);
                resultJsonObject.put(key, depJson);
            }else {
                resultJsonObject.put(key, new JSONObject());
            }
        } else if (FieldTypeEnum.DEPT_GROUP.getType().equals(fieldAttrEntity.getFieldType())) {
            List<DepartmentEntity> departmentList = UserHelper.getDepartmentByKey(key, objectJsonObject, departmentMap);
            List<JSONObject> nameList = new ArrayList<>();
            departmentList.forEach(item->{
                JSONObject json = new JSONObject();
                json.put("id", item.getId());
                json.put("name", item.getName());
                nameList.add(json);
            });
            resultJsonObject.put(key, nameList);
        } else {
            super.analysisData(analysisDataDTO, resultJsonObject, linkData, linkDataSet);
        }
    }

}
